package applet.logic;

import java.io.IOException;
import java.util.HashMap;

import applet.configurationManagement.Configuration;

public class MediaModule {
	
	Configuration _config = null;
	HashMap<String, MediaManager> _managers = null;

	public MediaModule(Configuration config) {
		_managers = new HashMap<String, MediaManager>();
		_config = config;
	}

	public void addMediaManager(String media){
		MediaManager m = new MediaManager(_config.get_address(),_config.get_receivingport(), _config.get_playerport(),_config.get_sendinglocalport());
		_managers.put(media, m);
		m.start();
		try {
			Runtime.getRuntime().exec(_config.get_vlcPath() + " rtp://@"+ _config.get_address()+":"+_config.get_playerport());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public MediaManager getMediaManager(String media){
		return _managers.get(media);
	}
	
	public void removeMediaManager(String media){
		_managers.get(media).terminate();
		_managers.remove(media);
	}

	public void sendTo(String address, String media){
		if ((address.charAt(0)==('['))&&address.charAt(address.length()-1)==(']'))
			address = address.substring(1,address.length()-2);
		String host =address.substring(0,address.indexOf(":"));
		int port = Integer.parseInt(address.substring(address.indexOf(":")+1));
		_managers.get(media).add_forward(host, port);
	}
	
	public void sendTo(String address, int port, String media){
		_managers.get(media).add_forward(address,port);
	}
	
}
